/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Description:TODO(功能)
 * @className AdminDaoImpl.java
 * @Package: page16 
 * @author: yj   
 * @date: 2019年1月16日 上午10:48:02 
 */
package com.erp.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.po.Admin;
import com.erp.po.Categorys;
import com.erp.po.Products;
import com.erp.po.Providers;
import com.erp.util.Page;

/**
 * @className AdminDaoImpl.java
 * @author yj
 * @Description:TODO( 管理员 Dao层的实现 )
 * @Date 2019年1月16日
 */
public class AdminDaoImpl extends BaseDao implements AdminDao {

	/*
	 * ( 管理员注册 )
	 * 
	 */
	@Override
	public int insertAdmin(Connection conn, Admin admin) throws SQLException {
		// String sql = "insert into admin (admin_name,admin_password,date,tel) values (?,?,now(),?)"; // mysql
		String sql = "insert into admin (admin_id,admin_name,admin_password,date,tel) values (ADMIN_SEQ.nextval,?,?,sysdate,?)"; // oracle
		Object[] obj = { admin.getAdminName(), admin.getAdminPassword(), admin.getTel() };
		return super.updateData(conn, sql, obj);
	}

	/*
	 * (删除)
	 * 
	 */
	@Override
	public int deleteAdminById(Connection conn, Integer Id) throws SQLException {
		String sql = "delete from admin where admin_id = ?";
		Object[] obj = { Id };
		return super.updateData(conn, sql, obj);
	}

	/*
	 * (修改)
	 * 
	 */
	@Override
	public int updateAdminById(Connection conn, Admin admin) throws SQLException {
		String sql = "update admin set admin_name = ? ,admin_password = ? ,tel = ? where admin_id = ?";
		Object[] obj = { admin.getAdminName(), admin.getAdminPassword(), admin.getTel(), admin.getAdminId() };
		return super.updateData(conn, sql, obj);
	}

	/*
	 * (管理员登录)
	 * 
	 */
	@Override
	public ResultSet selectAdminByLogin(Connection conn, Admin admin) throws SQLException {
		String sql = "select * from admin where admin_name = ? and admin_password = ?";
		return super.selectData(conn, sql, admin.getAdminName(), admin.getAdminPassword());
	}

	/*
	 * (分页查询)
	 * 
	 */
	@Override
	public ResultSet selectAdminByPage(Connection conn, Page<Admin> page) throws SQLException {
		// String sql = "select * from admin limit ?,? "; // mysql
		String sql = "select * from (select rownum rn, admin.* from admin where rownum < ?) temp where temp.rn > ? "; //oracle
		// Object[] obj = { ((page.getCurrentPage() - 1) * page.getPageSize()), page.getPageSize() };
		Object[] obj = { (page.getCurrentPage() * page.getPageSize()), ((page.getCurrentPage() - 1) * page.getPageSize()) };
		return super.selectData(conn, sql, obj);
	}

	/*
	 * (查询总记录数 )
	 * 
	 */
	@Override
	public int getTotal(Connection conn) throws SQLException {
		int count = 0;
		String sql = "select count(*) from admin";
		ResultSet set2 = super.selectData(conn, sql, null);
		while (set2.next()) {
			count = set2.getInt(1);
		}
		return count;
	}

	/*
	 * (查( 是否又重复值 ))
	 * 
	 */
	@Override
	public ResultSet selectAdminByAdd(Connection conn, Admin admin) throws SQLException {
		String sql = "select * from admin where admin_name = ? ";
		return super.selectData(conn, sql, admin.getAdminName());
	}

	
	
	

}
